/**
 * 初始化协议明细详情对话框
 */
var deptSelect = {
    deptSelectData : {}
};

/**
 * 关闭此对话框
 */
deptSelect.close = function() {
    $("#menuContent").css("display","none");
}

/**
 * 点击部门input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
deptSelect.onClickDept = function (e, treeId, treeNode) {
    $("#deptName").attr("value", instance.getSelectedVal());
    $("#deptId").attr("value", treeNode.id);
};

/**
 * 显示部门选择的树
 *
 * @returns
 */
deptSelect.showDeptSelectTree = function () {
    var cityObj = $("#deptName");
    var cityOffset = $("#deptName").offset();
    $("#menuContent").css({
        left: cityOffset.left + "px",
        top: cityOffset.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 显示用户详情部门选择的树
 *
 * @returns
 */
deptSelect.showInfoDeptSelectTree = function () {
    var cityObj = $("#deptName");
    var cityPosition = $("#deptName").position();
    $("#menuContent").css({
        left: cityPosition.left + "px",
        top: cityPosition.top + cityObj.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};

/**
 * 隐藏部门选择的树
 */
deptSelect.hideDeptSelectTree = function () {
    $("#menuContent").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(
            event.target).parents("#menuContent").length > 0)) {
        deptSelect.hideDeptSelectTree();
    }
}

$(function() {
    var menuHtml = '<div id=\"menuContent\" class=\"menuContent\" style=\"display: none; position: absolute; z-index: 200;\"> <ul id=\"treeDemo\" class=\"ztree tree-box\" style=\"width: 249px !important;\"></ul></div>';
    $("body").prepend(menuHtml);
    var ztree = new $ZTree("treeDemo", "/dept/tree");
    ztree.bindOnClick(deptSelect.onClickDept);
    ztree.init();
    instance = ztree;
});
